﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace Pp.UI
{
    using Binders;
    using Core.FluentNH;

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Reklam", id = UrlParameter.Optional } // Parameter defaults
            );

        }
        protected static void RegisterModelBinders(ModelBinderDictionary binders)
        {
            // Default binder
            binders.DefaultBinder = new DefaultModelBinder();

            // others
            Type[] assemblyTypes = typeof(MvcApplication).Assembly.GetTypes();
            foreach (var binder in
                from type in assemblyTypes
                where type.IsSubclassOf(typeof(ModelBinder))
                select Activator.CreateInstance(type))
            {
                binders.Add(((ModelBinder)binder).Handles, (IModelBinder)binder);
            }
        }
        protected virtual void Application_Error(object sender, EventArgs e)
        {
            var exception = Server.GetLastError();
            Response.Clear();
            Response.ClearContent();
            Response.ContentType = "text/html";
            Response.Write("Sistem şuan için kullanılamamaktadır.Lütfen daha sonra tekrar deneyiniz yada yöneticilerden biri ile iletişime geçiniz.");
            Response.Write("<p style='display:none;'>" + exception + "</p>");
            Response.End();
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterModelBinders(ModelBinders.Binders);
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}